package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.OrderQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.Date;

/**
 * 对象功能:  合同列表查询
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/7/25 15:42
 */
public class ContractQueyFragment extends BaseFragment {
  @BindView(R.id.slv_startTime) SingleLineView slvStartTime;
  @BindView(R.id.slv_endTime) SingleLineView slvEndTime;
  @BindView(R.id.slv_outState) SingleLineView slvContractName;
  @BindView(R.id.slv_customerName) SingleLineView slvContractCode;
  private OrderQueryParams mParams;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    mParams = (!(mBaseParams instanceof OrderQueryParams)) ? new OrderQueryParams()
        : (OrderQueryParams) mBaseParams;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        mParams.setName(slvContractName.getTextContent());
        mParams.setCode(slvContractCode.getTextContent());
        Intent intent = new Intent();
        intent.putExtra(EXTRA_PARAMS, mParams);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_order_query, container, false);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    slvStartTime.getTvContent().setText(mParams.getBeginregDate());
    slvEndTime.getTvContent().setText(mParams.getEndregDate());
    slvContractName.setType(SingleLineView.NORMAL);
    slvContractName.setTextTitle("合同名称:");
    slvContractName.getTvContent().setText(mParams.getName());
    slvContractCode.setTextTitle("合同编号:");
    slvContractCode.getTvContent().setText(mParams.getCode());

    slvStartTime.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(
            BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(mParams.getBeginregDate()))
                .setCallBack(new BaseYMDTimeDialogFragment.CallBack() {
                  @Override public void select(Date date) {
                    mParams.setBeginregDate(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
                    slvStartTime.getTvContent()
                        .setText(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
                  }
                }));
      }
    });
    slvEndTime.getTvContent().setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(mParams.getEndregDate()))
            .setCallBack(new BaseYMDTimeDialogFragment.CallBack() {
              @Override public void select(Date date) {
                mParams.setEndregDate(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
                slvEndTime.getTvContent().setText(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));
              }
            }));
      }
    });
  }
}